package cn.heyige.backend.controller;


import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.DivinationResult;
import cn.heyige.backend.dto.PersonalizedTimeDivinationRequest;
import cn.heyige.backend.service.DivinationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/divination")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "占卜接口", description = "梅花易数占卜相关接口")
public class DivinationController {
    
    private final DivinationService divinationService;
    
    
    /**
     * 时间起卦接口
     * @param request 时间起卦请求参数
     * @return 占卜结果
     */
    @PostMapping("/time")
    @Operation(summary = "时间起卦", description = "根据时间戳生成卦象")
    public CommResponse<DivinationResult> timeDivination(@RequestBody TimeDivinationRequest request) {
        try {
            log.info("时间起卦请求，时间戳: {}", request.getTimestamp());
            DivinationResult result = divinationService.performTimeDivination(request.getTimestamp());
            return CommResponse.ok(result);
        } catch (Exception e) {
            log.error("时间起卦失败", e);
            return CommResponse.fail(500, "时间起卦失败: " + e.getMessage());
        }
    }
    
    /**
     * 个性化时间起卦接口（使用增强版算法）
     * @param request 个性化时间起卦请求参数
     * @return 占卜结果
     */
    @PostMapping("/personalized-time")
    @Operation(summary = "个性化时间起卦", description = "根据时间戳和用户信息生成个性化卦象")
    public CommResponse<DivinationResult> personalizedTimeDivination(@RequestBody PersonalizedTimeDivinationRequest request) {
        try {
            log.info("个性化时间起卦请求，时间戳: {}, 用户ID: {}, 问题: {}", 
                    request.getTimestamp(), request.getUserId(), request.getQuestion());
            
            // 统一使用增强版个性化算法
            DivinationResult result = divinationService.performPersonalizedTimeDivination(
                request.getTimestamp(),
                request.getUserId(),
                request.getQuestion()
            );
            
            return CommResponse.ok(result);
        } catch (Exception e) {
            log.error("个性化时间起卦失败", e);
            return CommResponse.fail(500, "个性化时间起卦失败: " + e.getMessage());
        }
    }
    
    /**
     * 数字起卦接口
     * @param request 数字起卦请求参数
     * @return 占卜结果
     */
    @PostMapping("/number")
    @Operation(summary = "数字起卦", description = "根据两个数字生成卦象")
    public CommResponse<DivinationResult> numberDivination(@RequestBody NumberDivinationRequest request) {
        try {
            log.info("数字起卦请求，数字1: {}, 数字2: {}", request.getNumber1(), request.getNumber2());
            DivinationResult result = divinationService.performNumberDivination(request.getNumber1(), request.getNumber2());
            return CommResponse.ok(result);
        } catch (Exception e) {
            log.error("数字起卦失败", e);
            return CommResponse.fail(500, "数字起卦失败: " + e.getMessage());
        }
    }
    
    /**
     * AI解卦接口
     * @param request AI解卦请求参数
     * @return AI解卦结果
     */
    @PostMapping("/ai-interpret")
    @Operation(summary = "AI解卦", description = "使用AI模型对卦象进行解读")
    public CommResponse<String> aiInterpret(@RequestBody AiInterpretRequest request) {
        try {
            log.info("AI解卦请求，本卦: {}, 变卦: {}, 问题: {}, AI提供商: {}",
                     request.getOriginalName(), request.getChangedName(), request.getQuestion(), request.getAiProvider());
            String result = divinationService.interpretDivination(
                request.getOriginalName(), request.getChangedName(), request.getQuestion(), request.getAiProvider());
            return CommResponse.ok(result);
        } catch (Exception e) {
            log.error("AI解卦失败", e);
            return CommResponse.fail(500, "AI解卦失败: " + e.getMessage());
        }
    }
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TimeDivinationRequest {
        @Parameter(description = "时间戳")
        private Long timestamp;
    }
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class NumberDivinationRequest {
        @Parameter(description = "第一个数字")
        private Integer number1;
        @Parameter(description = "第二个数字")
        private Integer number2;
    }
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AiInterpretRequest {
        @Parameter(description = "本卦名称")
        private String originalName;
        
        @Parameter(description = "变卦名称")
        private String changedName;
        
        @Parameter(description = "用户问题（可选）")
        private String question;
        
        @Parameter(description = "AI服务提供商（可选，支持zhipu和qwen）")
        private String aiProvider;
    }
}